package com.suliao.library.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.suliao.common.base.Result;
import com.suliao.library.entity.User;
import com.suliao.library.service.UserService;
import com.suliao.home.entity.content.CompanyDO;
import com.suliao.home.service.content.CompanyService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.List;


/**
 * @author： M1nG-CodeGenerator
 * @since： 2020-09-10 17:35:51
 * @description： 用户 前端控制器
 * @version： 1.0
 */
@Api("User API接口")
@RestController
@RequestMapping(value = "/")
@Slf4j
public class UserController {

    @Reference()
    public UserService userService;
    @Reference()
    public CompanyService companyService;

    @RequestMapping("getMsg")
    public String  getMsg(HttpSession session) {
        String msg=(String) session.getAttribute("msg");
        return msg;
    }


    @ApiOperation("RESTFul API 通过id获取用户信息")
    @ApiImplicitParam(name = "id", value = "用户ID", paramType = "path", required = true, dataType = "Integer")
    @GetMapping("/user/{id}")
    public Result getUser(@PathVariable("id")Integer userId) {
        Result result = Result.ok();
        try {
            Page<User> user = userService.getUserById(new Page(1,10), userId);
            result.setData(user);
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
            result = Result.error("系统错误");
        }
        return result;
    }

    @ApiOperation("获取公司信息")
    @GetMapping("/company")
    public Result<CompanyDO> getCompany() {
        Result result = Result.ok();
        try {
            CompanyDO company = companyService.getCompanyById(1);
            result.setData(company);
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
            result = Result.error("系统错误");
        }
        return result;
    }

    @ApiOperation("慢sql测试")
    @GetMapping("/name")
    public Result getUsert() {
        Result result = Result.ok();
        try {
            List<User> userByName = userService.getUserByName();
            result.setData(userByName);
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
            result = Result.error("系统错误");
        }
        return result;
    }

}
